package com.team16.budgetme;

import java.text.NumberFormat;
import java.util.ArrayList;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockListActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;

public class HistoryActivity extends SherlockListActivity {
    private DataHandler data;
    private ArrayList<Account> accounts;
    private ArrayList<Transaction> transactions;
    private int position;
    private Bundle extras;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        data = new DataHandler(this);
        accounts = data.getAllAccounts();
        extras = getIntent().getExtras();
        position = extras.getInt("position");
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
    }

    @Override
    protected void onStart() {
        super.onStart();

        String name = accounts.get(position).getName();

        if (data.isTransactionsEmpty(name)) {
            setContentView(R.layout.no_transactions);
        } else {
            setContentView(R.layout.activity_history);

            transactions = data.getAllTransactions(accounts.get(position).getName());

            ArrayAdapter<Transaction> adapter = new HistoryAdapter(this, R.layout.history_list_row,
                    transactions);
            setListAdapter(adapter);
                    
            getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                    AlertDialog dialog = deleteTransactionDialog(arg1, arg2);
                    dialog.show();
                }
            });
            
            /*  
            getListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
                @Override
                public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                    AlertDialog dialog = deleteTransactionDialog(arg1, arg2);
                    dialog.show();
                    return false;
                }
            });
            */
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getSupportMenuInflater().inflate(R.menu.history, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        Intent intent;
        switch (item.getItemId()) {
            case android.R.id.home:
                intent = new Intent(this, MainActivity.class);
                intent.putExtra("postion", position);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
                return true;
            /*
            case R.id.graph:
                intent = new Intent(this, GraphActivity.class);
                startActivity(intent);
                return true;
            */
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    private AlertDialog deleteTransactionDialog(View view, final int position) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        LayoutInflater inflater = getLayoutInflater();
        View layout = inflater.inflate(R.layout.delete_transaction_dialog, null);
        
        builder.setTitle(R.string.delete_transaction)
                .setView(layout)
                .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        deleteTransaction(position);                       
                    }
                })
                .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });
        
        return builder.create();
    }
    
    private void deleteTransaction(int index) {
        System.err.println(index);
        System.err.println(transactions.get(index).getId());
        data.deleteTransaction(transactions.get(index));
        this.onStart();
    }
    
    private class HistoryAdapter extends ArrayAdapter<Transaction> {
        Context context;
        ArrayList<Transaction> transactions;

        public HistoryAdapter(Context context, int resource, ArrayList<Transaction> transactions) {
            super(context, resource, transactions);
            this.context = context;
            this.transactions = transactions;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflator = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View layout = inflator.inflate(R.layout.history_list_row, parent, false);
            Transaction t = transactions.get(position);
            TextView date = (TextView) layout.findViewById(R.id.date);
            TextView amount = (TextView) layout.findViewById(R.id.amount);
            TextView category = (TextView) layout.findViewById(R.id.category);
            
            if(t.getType() == 1) {
                layout.setBackgroundColor(Color.rgb(0, 100, 0));
            } else if (t.getType() == 0) {
                layout.setBackgroundColor(Color.rgb(100, 0, 0));
            }

            date.setText(t.getDate());
            amount.setText(NumberFormat.getCurrencyInstance().format(t.getAmount()));
            category.setText(t.getCategory());
            return layout;
        }
    }

}